package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.Impl.NewsCatalogueDaoImpl;
import com.ysd.entity.NewsCatalogue;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminNewsCatalogueServlet extends BaseServlet {
	/****
	 * 获得新闻分类
	 * localhost:8080/huaduoyoupin/adminNewsCatalogue?reqName=getNewsCatalogue
	 */
	public void getNewsCatalogue(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		NewsCatalogueDaoImpl nDao=new NewsCatalogueDaoImpl();//创建实现类对象
		int page=Integer.parseInt(request.getParameter("page"));
		int size=Integer.parseInt(request.getParameter("size"));
		
		List<NewsCatalogue> nList=nDao.getNewCataloguePage(page, size);//获得所有新闻分类
		
		response.setCharacterEncoding("UTF-8");//设置响应编码格式
		PrintWriter out=response.getWriter();//out对象
		
		Gson gson=new Gson();//创建gson对象
		
		String res=gson.toJson(nList);//将pcList转化为json格式的字符串
		out.print(res);//响应数据
		
		
		out.flush();
		out.close();
	}
	//分页查询
	//localhost:8080/huaduoyoupin/adminNewsCatalogue?reqName=getAllNewsCatalogue
	public void getAllNewsCatalogue(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		NewsCatalogueDaoImpl nDao=new NewsCatalogueDaoImpl();//创建实现类对象
		List<NewsCatalogue> nList=nDao.getNewsList();//获得所有新闻分类
		
		response.setCharacterEncoding("UTF-8");//设置响应编码格式
		PrintWriter out=response.getWriter();//out对象
		
		Gson gson=new Gson();//创建gson对象
		
		String res=gson.toJson(nList.size());//将pcList转化为json格式的字符串
		out.print(res);//响应数据
		
		
		out.flush();
		out.close();
	}
	
	/*****
	 * 删除新闻分类
	 */
	public void delNewsCatalogue(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//接收前台传来的参数
		int id=Integer.parseInt(request.getParameter("newsId"));
		NewsCatalogueDaoImpl nDao=new NewsCatalogueDaoImpl();//创建实现类
		
		//设置编码集格式
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		//调用实现类的删除方法，返回受影响行数
		int n=nDao.delNewsCatalogue(id);
		if(n>0) {
			out.print(Result.toClient(true, "删除成功",""));
		}else {
			out.print(Result.toClient(false, "删除失败",""));
		}
		
		
		out.flush();
		out.close();
	}
	
	/****
	 * 添加新闻分类
	 * localhost:8080/huaduoyoupin/adminNewsCatalogue?reqName=addNewsCatalogue
	 */
	public void addNewsCatalogue(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		NewsCatalogueDaoImpl ncDao=new NewsCatalogueDaoImpl();//调用实现类创建对象
		
		String newsCatalogueName=request.getParameter("newsName");//接收前台传来的参数(随便起的参数名)
		if(newsCatalogueName!=null&&!newsCatalogueName.equals("")) {
			List<NewsCatalogue> ncList=ncDao.getNewsCatalogueByName(newsCatalogueName);
			if(ncList.size()==0) {
				NewsCatalogue nc=new NewsCatalogue(0,newsCatalogueName);
				System.out.print(nc);
				int n=ncDao.addNewsCatalogue(nc);
				if(n>0) {
					out.print(Result.toClient(true, "添加成功",""));
				}else {
					out.print(Result.toClient(false, "添加失败",""));
				}
			}else {
				out.print(Result.toClient(false, "分类名已存在",""));
			}
		}
		
		
		out.flush();
		out.close();
	}
/**
 * localhost:8080/huaduoyoupin/adminNewsCatalogue?reqName=getNewsCatalogueById
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
	
	public void getNewsCatalogueById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		NewsCatalogueDaoImpl ncDao=new NewsCatalogueDaoImpl();//调用实现类创建对象
		int cataId=Integer.parseInt(request.getParameter("nId"));
		NewsCatalogue n= ncDao.getNewsCatalogueById(cataId);
		
		Gson gson=new Gson();
		String res=gson.toJson(n);
		out.print(res);
		
		out.flush();
		out.close();
	}
	
	/****
	 *  localhost:8080/huaduoyoupin/adminNewsCatalogue?reqName=updateNewsCatalogueById
	 * 修改新闻分类名称
	 */
	public void updateNewsCatalogueById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		NewsCatalogueDaoImpl ncDao=new NewsCatalogueDaoImpl();//调用实现类创建对象
		int newsCatalogueId=Integer.parseInt(request.getParameter("newsCatalogueId"));
		String newsCatalogueName=request.getParameter("newsCatalogueName");
		if(newsCatalogueName!=null&&!newsCatalogueName.equals("")) {
			List<NewsCatalogue> ncList=ncDao.getNewsCatalogueByName(newsCatalogueName);
			if(ncList.size()==0) {
				NewsCatalogue nc=new NewsCatalogue(newsCatalogueId, newsCatalogueName);
				int n=ncDao.updateNewsCatalogueById(nc);
				if(n>0) {
					out.print(Result.toClient(true, "修改成功",""));
				}else {
					out.print(Result.toClient(false, "修改失败",""));
				}
			}else {
				out.print(Result.toClient(false, "分类名称已存在",""));
			}
			
		}

		out.flush();
		out.close();
	}
	//批量删除
	// localhost:8080/huaduoyoupin/adminNewsCatalogue?reqName=delectNewsCatalogueByIds
	public void delectNewsCatalogueByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		NewsCatalogueDaoImpl nDao=new NewsCatalogueDaoImpl();//创建实现类
		
		//设置编码集格式
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		String ids=request.getParameter("ids");
		int n=nDao.delectProductByIds(ids);
		//调用实现类的删除方法，返回受影响行数
		if(n>0) {
			out.print(Result.toClient(true, "删除成功",""));
		}else {
			out.print(Result.toClient(false, "删除失败",""));
		}
		
		
		out.flush();
		out.close();
	}
	
}
